package com.mall.web.controller.product;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.product.domain.entity.ProType;
import com.mall.product.service.ProTypeService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 商品类型Controller
 *
 * @author zhonglin
 * @date 2020-05-07
 */
@Controller
@RequestMapping("/product/type")
public class ProTypeController
    extends BaseController {

  private String prefix = "product/type";

  @Autowired
  private ProTypeService proTypeService;

  @RequiresPermissions("product:type:view")
  @GetMapping()
  public String type() {

    return prefix + "/type";
  }

  /**
   * 查询商品类型列表
   */
  @RequiresPermissions("product:type:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(ProType proType) {

    startPage();
    List<ProType> list = proTypeService.selectProTypeList(proType);
    return getDataTable(list);
  }

  /**
   * 导出商品类型列表
   */
  @RequiresPermissions("product:type:export")
  @Log(title = "商品类型", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(ProType proType) {

    List<ProType>      list = proTypeService.selectProTypeList(proType);
    ExcelUtil<ProType> util = new ExcelUtil<ProType>(ProType.class);
    return util.exportExcel(list, "type");
  }

  /**
   * 新增商品类型
   */
  @GetMapping("/add")
  public String add() {

    return prefix + "/add";
  }

  /**
   * 新增保存商品类型
   */
  @RequiresPermissions("product:type:add")
  @Log(title = "商品类型", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(ProType proType) {

    return toAjax(proTypeService.insertProType(proType));
  }

  /**
   * 修改商品类型
   */
  @GetMapping("/edit/{typeId}")
  public String edit(@PathVariable("typeId") Long typeId,
                     ModelMap mmap) {

    ProType proType = proTypeService.selectProTypeById(typeId);
    mmap.put("proType", proType);
    return prefix + "/edit";
  }

  /**
   * 修改保存商品类型
   */
  @RequiresPermissions("product:type:edit")
  @Log(title = "商品类型", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(ProType proType) {

    return toAjax(proTypeService.updateProType(proType));
  }

  /**
   * 删除商品类型
   */
  @RequiresPermissions("product:type:remove")
  @Log(title = "商品类型", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {

    return toAjax(proTypeService.deleteProTypeByIds(ids));
  }

  /**
   * 查询商品类型详细
   */
  @RequiresPermissions("product:type:list")
  @GetMapping("/detail/{dictId}")
  public String detail(@PathVariable("dictId") Long typeId,
                       ModelMap mmap) {

    mmap.put("type", proTypeService.selectProTypeById(typeId));
    mmap.put("typeList", proTypeService.selectProTypeAll());
    return "product/customRecord/customRecord";
  }

}
